<template>
  <div class="app-container">
<!--    查询菜单-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="事件类型：" prop="eventType" >
        <el-select v-model="queryParams.eventType" placeholder="事件类型" style="width: 220px" @keyup.enter="handleQuery">
          <el-option label="考试" value="考试" />
          <el-option label="面试" value="面试" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="timeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template #default="scope">
          <span>{{scope.row.startDate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template #default="scope">
          <span>{{scope.row.endDate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="每天开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{scope.row.startTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="每天结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{scope.row.endTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      v-model:current-page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 30, 40]"
      @size-change="getList"
      @current-change="getList"
      class="custom-pagination"
    />

    <!-- 添加或修改考试及面试时间对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form ref="timeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="时间类型" prop="eventType">
          <el-select v-model="form.eventType" placeholder="时间类型" style="width: 220px">
            <el-option label="考试" value="考试" />
            <el-option label="面试" value="面试" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date" style="width: 220px"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date" style="width: 220px"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker clearable
            v-model="form.startTime"
            type="date" style="width: 220px"
            value-format="HH:mm:ss"
            placeholder="请选择每天开始时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-picker clearable
            v-model="form.endTime"
            type="date" style="width: 220px"
            value-format="HH:mm:ss"
            placeholder="请选择每天结束时间">
          </el-time-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="TimeManager">
import { listTime, getTime, delTime, addTime, updateTime } from "@/apis/system/time";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'


const { proxy } = getCurrentInstance() as { proxy:any};

const timeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<number[]>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {
    id: null,
    eventType: null,
    startDate: null,
    endDate: null,
    startTime: null,
    endTime: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    eventType: null,
    startDate: null,
    endDate: null,
    startTime: null,
    endTime: null
  },
  rules: {
    eventType: [
      { required: true, message: "事件类型：考试/免试后面试/面试不能为空", trigger: "blur" }
    ],
    startDate: [
      { required: true, message: "开始日期不能为空", trigger: "blur" }
    ],
    endDate: [
      { required: true, message: "结束日期不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "每天开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "每天结束时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询考试及面试时间列表 */
function getList() {
  loading.value = true;
  listTime(queryParams.value).then(response => {
    timeList.value = response.data.data.records;
    total.value = response.data.data.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    eventType: null,
    startDate: null,
    endDate: null,
    startTime: null,
    endTime: null
  }
  handleQuery();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    eventType: null,
    startDate: null,
    endDate: null,
    startTime: null,
    endTime: null
  };
  // proxy.resetForm("timeRef");
}

// 多选框选中数据
function handleSelectionChange(selection: any[]) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考试及面试时间";
}

/** 修改按钮操作 */
function handleUpdate(row: any) {
  reset();
  const _id = row.id || ids.value
  getTime(_id).then(response => {
    form.value = response.data.data;
    open.value = true;
    title.value = "修改考试及面试时间";
  });
}

/** 提交按钮 */
function submitForm() {
  (proxy.$refs["timeRef"] as any).validate((valid: boolean) => {
    if (valid) {
      if (form.value.id != null) {
        updateTime(form.value).then(response => {
          if (response.data.code === 200){
            ElMessage({ type: 'success', message: '更新成功' })
          }else {
            ElMessage({ type: 'error', message: response.data.msg })
          }
          open.value = false;
          getList();
        });
      } else {
        addTime(form.value).then(response => {
          if (response.data.code === 200){
            ElMessage({ type: 'success', message: '添加成功' })
          }else {
            ElMessage({ type: 'error', message: response.data.msg })
          }
          open.value = false;
          getList();
        });
      }
    }
  });
}


/** 删除按钮操作 */
function handleDelete(row:any) {
  const _ids = row.id || ids.value;
  ElMessageBox.alert('是否确认删除？', '提示', {
    confirmButtonText: '确定',
    callback: (action: string) => {
      if (action === 'confirm') {
        delTime(_ids).then(response => {
          if(response.data.code === 200){
            ElMessage({ type: 'success', message: '删除成功' })
            getList();
          }
        })
      }
    }
  });
}

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/time/export', {
//     ...queryParams.value
//   }, `time_${new Date().getTime()}.xlsx`)
// }

getList();
</script>
<style scoped>
.custom-pagination {
  margin-top: 20px; /* 调整分页组件与表格的间距 */
  display: flex;
  justify-content: center; /* 居中对齐分页组件 */
}

.custom-pagination .el-pagination__total,
.custom-pagination .el-pagination__sizes,
.custom-pagination .el-pager li,
.custom-pagination .btn-prev,
.custom-pagination .btn-next,
.custom-pagination .el-pagination__jump {
  font-size: 14px; /* 调整字体大小 */
  color: #333; /* 调整文字颜色 */
}

.custom-pagination .el-pagination__sizes .el-input .el-input__inner {
  height: 30px; /* 调整输入框高度 */
  line-height: 30px; /* 调整输入框行高 */
}

.custom-pagination .el-pager li {
  min-width: 30px; /* 调整分页按钮最小宽度 */
  height: 30px; /* 调整分页按钮高度 */
  line-height: 30px; /* 调整分页按钮行高 */
  background-color: #f5f7fa; /* 调整分页按钮背景颜色 */
  border-radius: 4px; /* 调整分页按钮圆角 */
  margin: 0 5px; /* 调整分页按钮间距 */
}

.custom-pagination .el-pager li.active {
  background-color: #409eff; /* 调整激活分页按钮背景颜色 */
  color: #fff; /* 调整激活分页按钮文字颜色 */
}

.custom-pagination .btn-prev,
.custom-pagination .btn-next {
  height: 30px; /* 调整前后按钮高度 */
  line-height: 30px; /* 调整前后按钮行高 */
  background-color: #f5f7fa; /* 调整前后按钮背景颜色 */
  border-radius: 4px; /* 调整前后按钮圆角 */
  margin: 0 5px; /* 调整前后按钮间距 */
}

.custom-pagination .btn-prev:hover,
.custom-pagination .btn-next:hover {
  background-color: #409eff; /* 调整前后按钮悬停背景颜色 */
  color: #fff; /* 调整前后按钮悬停文字颜色 */
}
</style>
